package com.dek.zk;

import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.Watcher;

public class CreateSession {

    private static class ClientHolder {
        private static ZkClient zkClient = CreateSession.connect();
    }

    public static ZkClient getClient() {
        return ClientHolder.zkClient;
    }

    private static ZkClient connect() {
        ZkClient zkClient = new ZkClient(Constants.SERVER_ADDRESS, 10000, 10000, new SerializableSerializer());
        zkClient.subscribeStateChanges(new IZkStateListener() {
            @Override
            public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception {
                System.out.println("handleStateChanged" + state);
            }

            @Override
            public void handleNewSession() throws Exception {
                System.out.println("handleNewSession");
            }

            @Override
            public void handleSessionEstablishmentError(Throwable error) throws Exception {
                System.out.println("handleSessionEstablishmentError");
            }
        });
        System.out.println("connected!");
        return zkClient;
    }



}
